package top.goldenyear.porpoise.framework.orm.core.converter;

import com.easy.query.core.basic.extension.complex.ComplexPropType;
import com.easy.query.core.basic.extension.conversion.ValueConverter;
import com.easy.query.core.metadata.ColumnMetadata;
import com.easy.query.core.util.EasyStringUtil;
import org.noear.snack.ONode;
import org.noear.snack.core.Options;

/**
 * @author airhead
 */
public class JsonConverter implements ValueConverter<Object, String> {

  @Override
  public String serialize(Object o, ColumnMetadata columnMetadata) {
    if (o == null) {
      return null;
    }

    return ONode.stringify(o, Options.def());
  }

  @Override
  public Object deserialize(String s, ColumnMetadata columnMetadata) {
    if (EasyStringUtil.isBlank(s)) {
      return null;
    }

    ComplexPropType complexType = columnMetadata.getComplexPropType();
    return ONode.deserialize(s, complexType.complexType());
  }
}
